|
1
|
1 |
|
const _ = require('lodash'), |
|
2
|
|
|
ParserInterface = require('./../ParserInterface') |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* @class SecuritySchemesParser |
|
6
|
|
|
* Security schema parser |
|
7
|
|
|
* |
|
8
|
|
|
* @property {Array.<SecurityObject>} schemes Security schemas config |
|
9
|
|
|
* @property {Array.<HeaderObject>} headers Security headers |
|
10
|
|
|
* @property {String} apiMethodLink Path to methods destination (without absolute path) |
|
11
|
|
|
* @property {Array.<ParameterObject>} parameters Security schemas parameters |
|
12
|
|
|
*/ |
|
13
|
|
|
class SecuritySchemesParser extends ParserInterface { |
|
14
|
|
|
/** |
|
15
|
|
|
* SecuritySchemaParser Constructor |
|
16
|
|
|
* |
|
17
|
|
|
* @param {Array.<SecurityObject>} schemes Schemes definition object |
|
18
|
|
|
*/ |
|
19
|
|
|
constructor (schemes) { |
|
20
|
5 |
|
super() |
|
21
|
|
|
|
|
22
|
5 |
|
this.schemes = schemes |
|
23
|
5 |
|
this.headers = {} |
|
24
|
5 |
|
this.parameters = {} |
|
25
|
|
|
|
|
26
|
5 |
|
this._parseParameters() |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Parse headers & parameters from schemes |
|
31
|
|
|
* |
|
32
|
|
|
* @private |
|
33
|
|
|
*/ |
|
34
|
|
|
_parseParameters () { |
|
35
|
5 |
|
let _self = this |
|
36
|
5 |
|
_.each(this.schemes, (config, name) => { |
|
37
|
|
|
|
|
38
|
5 |
|
if (typeof config !== 'object' || (typeof config === 'object' && typeof config.in === 'undefined')) { |
|
39
|
|
|
return |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
4 |
|
let title = (config.in === 'header' ? 'Header ' : 'Parameter ') + ' for authorization by ' + name |
|
43
|
|
|
|
|
44
|
4 |
|
let paramObject = { |
|
45
|
|
|
title : title, |
|
46
|
|
|
description : title, |
|
47
|
|
|
name : config.name, |
|
48
|
|
|
camelCaseName: _.camelCase(config.name), |
|
49
|
|
|
schemaName : name, |
|
50
|
|
|
required : config.required || false, |
|
51
|
|
|
value : [config.default || ''], |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
4 |
|
paramObject['is' + _.upperFirst(config.in) + 'Parameter'] = true |
|
55
|
|
|
|
|
56
|
4 |
|
if (config.in === 'header') { |
|
57
|
2 |
|
if (typeof _self.headers[name] === 'undefined') { |
|
58
|
2 |
|
_self.headers[name] = [] |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
_self.headers[name].push(paramObject) |
|
62
|
|
|
} else { |
|
63
|
2 |
|
if (typeof _self.parameters[name] === 'undefined') { |
|
64
|
2 |
|
_self.parameters[name] = [] |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
_self.parameters[name].push(paramObject) |
|
68
|
|
|
} |
|
69
|
|
|
}) |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
parse () { |
|
73
|
3 |
|
return this.schemes |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get parameters for request |
|
78
|
|
|
* @param {Object} security Security object |
|
79
|
|
|
* @return {Array.<ParameterObject>} |
|
80
|
|
|
*/ |
|
81
|
|
|
getParametersForRequest (security) { |
|
82
|
4 |
|
return this._getHeadersOrParams(security, false) |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get headers for request |
|
87
|
|
|
* @param {Object} security Security object |
|
88
|
|
|
* @param {boolean} isHeader True if needed headers or false if need parameters |
|
89
|
|
|
* @return {Array<HeaderObject>|Array.<ParameterObject>} |
|
90
|
|
|
*/ |
|
91
|
|
|
_getHeadersOrParams (security, isHeader) { |
|
92
|
8 |
|
let options = [], |
|
93
|
|
|
_self = this |
|
94
|
|
|
|
|
95
|
8 |
|
isHeader = isHeader ? true : typeof isHeader === 'undefined' |
|
96
|
|
|
|
|
97
|
8 |
|
let optionName = isHeader ? 'headers' : 'parameters' |
|
98
|
|
|
|
|
99
|
8 |
|
if (!_.isObject(security)) { |
|
100
|
4 |
|
return [] |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
4 |
|
_.each(security, (config, name) => { |
|
104
|
4 |
|
if (typeof _self[optionName][name] === 'undefined') { |
|
105
|
2 |
|
return |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
2 |
|
_.each(_self[optionName][name], function (config) { |
|
109
|
2 |
|
options.push(config) |
|
110
|
|
|
}) |
|
111
|
|
|
}) |
|
112
|
|
|
|
|
113
|
4 |
|
return options |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get headers for request |
|
118
|
|
|
* @param {Object} security Security object |
|
119
|
|
|
* |
|
120
|
|
|
* @return {Array.<HeaderObject>} |
|
121
|
|
|
*/ |
|
122
|
|
|
getHeadersForRequest (security) { |
|
123
|
4 |
|
return this._getHeadersOrParams(security) |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @ignore module.exports |
|
129
|
|
|
* @ignore exports |
|
130
|
|
|
*/ |
|
131
|
|
|
|
|
132
|
|
|
module.exports = SecuritySchemesParser |